package com.open.pokergames.comm.userCenter

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.open.pokergames.R
import com.open.pokergames.base.activity.LoadRecycleViewBaseActivity
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.biz.entity.MemberEntity
import com.open.pokergames.biz.entity.MemberVo
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_white_bg_for_search.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class UserSearchActivity : LoadRecycleViewBaseActivity<MemberVo>(), OnSuperListener<MemberEntity> {
    var searchValue = ""
    var viewModel: BizViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_load_recycle_view_load_tip_white_bg_for_search)
        viewModel = BizViewModel()
        initView()
        setListener()
        softInputFocusable(inputEt)
    }

    private fun setListener() {
        cancelBtn.setOnClickListener { doCancel() }

        inputEt.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (s!!.isNotEmpty()) {
                    searchView?.visibility = View.VISIBLE
                    searchValueTv.text = s.toString()
                } else {
                    searchView?.visibility = View.GONE
                    searchValueTv.text = ""
                }

                retData()
            }
        })

        searchView.setOnClickListener {
            showLoadingDialog("努力加载中")
            searchValue = searchValueTv.text.toString()
            refreshData()
            searchView?.visibility = View.GONE
            searchValueTv.text = ""
        }
    }

    protected open fun doCancel() {
        finish()
    }

    abstract fun retData()

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_10
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        setAdapter()

        loadingRecyclerView.setLoadingMoreEnabled(false)
        loadingRecyclerView.setPullRefreshEnabled(false)

        loadingTipView.showEmpty("暂无数据", R.drawable.data_empty)
        loadingTipView.setLoadingViewBg(R.color.transparent)
    }

    abstract fun setAdapter()

    override fun loadData(pageVo: PageVo<MemberVo>) {
        val pageReq = PageReq<MemberVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val data = MemberVo()
        data.keywords = searchValue
        pageReq.data = data
        viewModel!!.searchUserByKeywords(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<MemberEntity>>) {

        if ("searchUser" != result.eventType) {
            return
        }
        hideLoadingDialog()
        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常",  R.drawable.data_empty)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<MemberEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.data.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.data_empty)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                setAdapterData(appPageData.data!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.data.size == 0) {
                tip("没有更多数据了")
            } else {
                updateAdapterData(appPageData.data!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    abstract fun setAdapterData(data: MutableList<MemberEntity>)

    abstract fun updateAdapterData(data: MutableList<MemberEntity>)
}